"""
Case Type   : cm_ctl
Case Name   : 执行cm_ctl pause执行前节点互信不正常
Create At   : 2023/04/10
Owner       : opentestcase012
Description :
    1.删除节点间的用户互信;
    2.执行cm_ctl pause;
    3.执行cm_ctl pause;
Expect      :
    1.执行成功
    2.执行失败,合理报错
    3.执行失败,合理报错
History     :
"""
import os
import time
import unittest

from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger
from yat.test import Node
from yat.test import macro

primary_sh = CommonSH('PrimaryDbUser')


# @unittest.skipIf(2 != primary_sh.get_node_num(), '非1+2环境不执行')
class DefinedResource(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.log.info(f'----{os.path.basename(__file__)} start----')
        self.common = Common()
        self.constant = Constant()
        self.pri_user = Node('PrimaryDbUser')
        self.pri_root = Node('PrimaryRoot')
        self.primary_sh = CommonSH('PrimaryDbUser')
        self.sta1_sh = CommonSH('Standby1DbUser')
        self.sta1_user = Node('Standby1DbUser')
        self.sta2_user = Node('Standby2DbUser')

    def test_cm(self):
        text = '----step1:删除节点间的用户互信; expect:删除成功----'
        self.log.info(text)
        rm_cmd = f'source {macro.DB_ENV_PATH};rm -rf ~/.ssh;'
        self.log.info(rm_cmd)
        rm_res = self.common.get_sh_result(self.pri_user, rm_cmd)
        self.assertEqual('', rm_res, "执行失败:" + text)

        time.sleep(5)
        text = '----step2:执行cm_ctl pause; expect:执行失败,合理报错----'
        self.log.info(text)
        cm_ctl_res = self.sta1_sh.exec_cm_ctl('pause')
        exp_msg = 'The ssh trust relationship may be abnormal on some nodes'
        self.assertIn(exp_msg, cm_ctl_res, '执行失败:' + text)

        text = '----step3:执行cm_ctl resume; expect:执行失败,合理报错----'
        self.log.info(text)
        cm_ctl_res = self.sta1_sh.exec_cm_ctl('resume')
        self.assertIn(exp_msg, cm_ctl_res, '执行失败:' + text)

    def tearDown(self):
        self.log.info('-----清理环境-----')
        text1 = '-----恢复集群节点间互信 expect:成功-----'
        self.log.info(text1)
        host_tuple = (self.pri_user.ssh_host, self.sta1_user.ssh_host,
                      self.sta2_user.ssh_host)
        params = {'-f': 'test_hosts'}
        ssh_res = self.primary_sh.exec_gs_sshexkey(macro.DB_SCRIPT_PATH,
                                                   *host_tuple, **params)

        text = '----恢复集群状态; expect:执行成功----'
        self.log.info(text)
        cm_ctl_res1 = self.primary_sh.exec_cm_ctl('resume')
        cm_ctl_res2 = self.primary_sh.exec_cm_ctl('start')
        cm_ctl_res3 = self.primary_sh.exec_cm_ctl('switchover', '-a')
        time.sleep(10)
        cm_ctl_res4 = self.primary_sh.exec_cm_ctl('query', '-Civ')

        self.assertIn('Successfully created SSH trust', ssh_res,
                      '执行失败:' + text1)
        self.assertIn(self.constant.cm_resume_success_msg, cm_ctl_res1,
                      '执行失败:' + text)
        self.assertIn(self.constant.cm_start_success_msg, cm_ctl_res2,
                      '执行失败:' + text)
        self.assertIn(self.constant.cm_switchover_success_msg, cm_ctl_res3,
                      '执行失败:' + text)
        self.assertIn(self.constant.CLUSTER_NORMAL_MSG, cm_ctl_res4,
                      "执行失败:" + text)
        self.log.info(f'----{os.path.basename(__file__)} finish----')
